package com.andy.blog.dal.service.impl;

import com.andy.blog.common.enums.DeleteTypeEnum;
import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.AlbumPhotoDO;
import com.andy.blog.dal.mapper.AlbumPhotoMapper;
import com.andy.blog.dal.service.AlbumPhotoDalService;
import com.andy.blog.dal.vo.AlbumPhotoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2024/4/2 11:31
 **/
@Service("albumPhotoDalService")
public class AlbumPhotoDalServiceImpl implements AlbumPhotoDalService {

    @Resource
    private AlbumPhotoMapper albumPhotoMapper;


    @Override
    public List<AlbumPhotoDO> selectList(AlbumPhotoVO albumPhotoVO) {
        LambdaQueryWrapper<AlbumPhotoDO> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Objects.nonNull(albumPhotoVO.getParentId()), AlbumPhotoDO::getParentId, albumPhotoVO.getParentId())
                .in(CollectionUtils.isNotEmpty(albumPhotoVO.getTypeList()), AlbumPhotoDO::getPhotoType, albumPhotoVO.getTypeList());
        return albumPhotoMapper.selectList(queryWrapper);
    }

    @Override
    public AlbumPhotoDO selectById(Long id) {
        return albumPhotoMapper.selectById(id);
    }

    @Override
    public void add(AlbumPhotoVO albumPhotoVO) {
        AlbumPhotoDO albumPhotoDO = TransformUtils.transform(albumPhotoVO, AlbumPhotoDO.class);

        albumPhotoDO.setCreateBy(SecurityUtils.getUsername());
        albumPhotoDO.setIsDeleted(DeleteTypeEnum.DELETE_N.getCode());

        try {
            albumPhotoMapper.insert(albumPhotoDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    @Override
    public void updateById(AlbumPhotoVO albumPhotoVO) {

        AlbumPhotoDO albumPhotoDO = TransformUtils.transform(albumPhotoVO, AlbumPhotoDO.class);

        albumPhotoDO.setUpdateBy(SecurityUtils.getUsername());

        try {
            albumPhotoMapper.updateById(albumPhotoDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }
    }

    @Override
    public void deleteById(Long id) {
        try {
            albumPhotoMapper.deleteById(id);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }
    }

    @Override
    public boolean checkExists(String photoType) {
        LambdaQueryWrapper<AlbumPhotoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AlbumPhotoDO::getParentId, 0L)
                .eq(AlbumPhotoDO::getPhotoType, photoType);
        Long row = albumPhotoMapper.selectCount(lambdaQueryWrapper);
        return row > 0;
    }
}
